package java0419;

import java.util.*;

class AA {
    public int a;
    public double b;

    public AA(int a, double b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public String toString() {
        return "a = " + a + "  b = " + b;
    }

    @Override
    public int hashCode() {
        Integer aInt = a;
        Double bDou = b;
        return aInt.hashCode() + bDou.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        AA aa = (AA)obj;
        if (aa.a == a && aa.b == b) {
            return true;
        }
        return false;
    }
}

class BB /*implements Comparable<BB> */{
    public int a;
    public double b;

    public BB(int a, double b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public String toString() {
        return "a= " + a + " b= " + b;
    }

//        @Override
//    public int compareTo(BB o) {
//        if (a == o.a) {
//            if (b == o.b) {
//                return 0;
//            }else if (b > o.b){
//                return 1;
//            }else {
//                return -1;
//            }
//        }else {
//            return a - o.a;
//        }
//    }
}

public class Test1 {
    public static void main(String[] args) {
//        AA aa1 = new AA(1, 20.4);
//        AA aa2 = new AA(1, 20.4);
//        System.out.println("aa1 的信息为" + aa1.toString());
//        System.out.println("aa2 的信息为" + aa2.toString());
//        System.out.println("aa1 的hash码为 " + aa1.hashCode());
//        System.out.println("aa2 的hash码为 " + aa2.hashCode());
//        System.out.println("aa1 和 aa2 是否相等 " + aa1.equals(aa2));
//        Set<AA> set = new HashSet<>();
//        set.add(aa1);
//        set.add(aa2);
//        System.out.println("set 中存放元素的个数为" + set.size());

        BB bb1 = new BB(1, 23.4);
        BB bb2 = new BB(6, 25.6);
        BB bb3 = new BB(1, 23.4);
        Set<BB> set = new TreeSet<>(new Comparator<BB>(

        ) {
            @Override
            public int compare(BB o1, BB o2) {
                if (o1.a == o2.a) {
                    if (o1.b == o1.b) {
                        return 0;
                    }else if (o1.b > o2.b) {
                        return 1;
                    }else {
                        return -1;
                    }
                }else {
                    return o1.a - o2.a;
                }
            }
        });
        set.add(bb1);
        set.add(bb2);
        set.add(bb3);
        System.out.println("TreeSet 测试");
        System.out.println(set.size());
        PriorityQueue<BB> priorityQueue = new PriorityQueue<>(new Comparator<BB>(

        ) {
            @Override
            public int compare(BB o1, BB o2) {
                if (o1.a == o2.a) {
                    if (o1.b == o1.b) {
                        return 0;
                    }else if (o1.b > o2.b) {
                        return 1;
                    }else {
                        return -1;
                    }
                }else {
                    return o1.a - o2.a;
                }
            }
        });
        priorityQueue.offer(bb1);
        priorityQueue.offer(bb2);
        priorityQueue.offer(bb3);
        System.out.println("优先级队列测试");
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        List<BB> list = new ArrayList<>();
        list.add(bb1);
        list.add(bb2);
        list.add(bb3);
        Collections.sort(list, new Comparator<BB>(

        ) {
            @Override
            public int compare(BB o1, BB o2) {
                if (o1.a == o2.a) {
                    if (o1.b == o1.b) {
                        return 0;
                    }else if (o1.b > o2.b) {
                        return 1;
                    }else {
                        return -1;
                    }
                }else {
                    return o1.a - o2.a;
                }
            }
        });
        System.out.println("排序测试");
        for (BB x : list) {
            System.out.println(x);
        }
    }
}
